//
//  DB.m
//  UI 19_数据库
//
//  Created by dllo on 15/5/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DB.h"

#import <sqlite3.h>
//确保数据库指针不管创建多少次,永远都是同一指针

static sqlite3 *dbPoint = nil;
@implementation DB

+ (sqlite3 *)openDB
{
    if (dbPoint) {
        return dbPoint;
    }
    

    // 判断Library中是否有数据库文件,如果没有则拷贝进去,如果有则什么也不做

    NSFileManager *maneger = [NSFileManager defaultManager];
  
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *Path = [array lastObject];
   Path = [NSString stringWithFormat:@"%@/%@", Path, @"DB1.rdb"];
    
    NSLog(@"%@", Path);
    BOOL judge = [maneger fileExistsAtPath:Path];
    if (judge) {
        
    }else {
          NSString *path = [[NSBundle mainBundle] pathForResource:@"music.rdb" ofType:nil];
        [maneger copyItemAtPath:path toPath:Path error:nil];
      
    }
    
    // 获得拷贝到的数据库文件的指针
    
    sqlite3_open([Path UTF8String], &dbPoint);
    return dbPoint;

}


+ (void)closeDB
{
    sqlite3_close(dbPoint);
}






@end
